const now = new Date(); // 当前日期
const nowDayOfWeek = now.getDay(); // 今天本周的第几天
//获取当前时间点
const nowDay = now.getDate(); // 当前日
const nowMonth = now.getMonth(); // 当前月
let nowYear = now.getYear(); // 当前年
nowYear += nowYear < 2000 ? 1900 : 0;
/**
 * @param 日期格式化
 * @returns {String}
 */
function formatDate(date) {
  const myyear = date.getFullYear();
  let mymonth = date.getMonth() + 1;
  let myweekday = date.getDate();
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return myyear + "-" + mymonth + "-" + myweekday;
}

/**
 * 获得当前日期
 *
 */
export function getNowDay() {
  return formatDate(new Date()) + ' 00:00:00';
}
export function getEndNowDay() {
    return formatDate(new Date()) + ' 23:59:59';
  }
/**
 * 获得本周的开始时间
 *
 */
export function getStartDayOfWeek() {
  const day = nowDayOfWeek || 7;
  return formatDate(
    new Date(now.getFullYear(), nowMonth, nowDay + 1 - day)
  ) + ' 00:00:00';
}

/**
 * 获得本周的结束时间
 *
 */
export function getEndDayOfWeek() {
  const day = nowDayOfWeek || 7;
  return formatDate(
    new Date(now.getFullYear(), nowMonth, nowDay + 7 - day)
  ) + ' 23:59:59';
}

/**
 * 获得本月的开始时间
 *
 */
export function getStartDayOfMonth() {
  const monthStartDate = new Date(nowYear, nowMonth, 1);
  return formatDate(monthStartDate) + ' 00:00:00';
}

/**
 * 获得本月的结束时间
 *
 */
export function getEndDayOfMonth() {
  const monthEndDate = new Date(nowYear, nowMonth, getMonthDays());
  return formatDate(monthEndDate) + ' 23:59:59';
}

/**
 * 获得本月天数
 *
 */
export function getMonthDays() {
  const monthStartDate = new Date(nowYear, nowMonth, 1);
  const monthEndDate = new Date(nowYear, nowMonth + 1, 1);
  return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
}

/**
 * 获取当前年份的第一天
 * @returns {string} 例如 2022-01-01
 */
export function getYearFirstDay() {
  const date = new Date();
  const year = date.getFullYear();
  return year + '-01-01'
}

/**
 * 获取当前年份的最后一天
 * @returns {string} 2022-12-31
 */
export function getYearLastDay(){
  const date = new Date();
  const year = date.getFullYear();
  return year + '-12-31'
}
